<?php

class Controller {

	protected static $rounter = array();
	protected static $controllerDir = '';

	public function __construct() {
		
	}

	public function go($module = '', $controller = '', $action = '') {
		$this->getRouter(); //开始解析URL获得请求的控制器和方法 
		$module = !empty($module) ? $module : $_GET['module'];
		$control = !empty($controller) ? $controller : $_GET['controller'];
		$action = !empty($action) ? $action : $_GET['action'];
		$control = ucwords($control . 'Action'); //将控制器名称中的每个单词首字母大写,来当作控制器的类名
		//面向对象编程还是面向过程编程 
		$oop_mode = (SysConfig::$OOP_MODE && !isset($_GET['pop'])) ? true : false;
		//这里构造出控制器文件的路径 
		$controlFile = $oop_mode ? self::$controllerDir . '/' . $module . '/' . $control . '.class.php' : self::$controllerDir . '/' . $module . '/' . $control . '.pop.php';
		if (!file_exists($controlFile)) { //如果文件不存在提示错误, 否则引入 
			die('the controller file of  ' . $controlFile . ' is not exsit');
		}

		include_once($controlFile);
		//如果设置为面向对象编程则自动执行ACTION
		if ($oop_mode) {
			if (!class_exists($control)) { //判断类是否存在, 如果不存在提示错误 
				die(' the class ' . $control . ' of controller is not exsit');
			}
			$instance = new $control(); //否则创建实例 
			$method = $action;
			if (!method_exists($instance, $method)) { //判断实例$instance中是否存在$action方法, 不存在则提示错误 
				die(' the action ' . $action . ' in  the class ' . $control . ' is not exsit');
			}
			//$instance->$method();
			call_user_func(array($instance, $method));   //或者
		}
	}

	protected function getRouter() {
		//如果URL模式为1那么就在GET中获取控制器, http://localhost/index.php?c=控制器&a=方法 
		if (SysConfig::$URL_MODE == 1) {
			$_GET['module'] = !empty($_GET['m']) ? trim($_GET['m']) : '';
			$_GET['controller'] = !empty($_GET['c']) ? trim($_GET['c']) : '';
			$_GET['action'] = !empty($_GET['a']) ? trim($_GET['a']) : '';
		}
		//如果为2那么就是使用PATH_INFO模式, http://localhost/index.php/控制器/方法/其他参数 
		elseif (SysConfig::$URL_MODE == 2) {
			if (!isset($_SERVER['PATH_INFO'])) {
				$_SERVER['PATH_INFO'] = $this->getPathInfo();
			}
			//print_r($_SERVER['PATH_INFO']);
			$paths = explode('/', trim($_SERVER['PATH_INFO'], '/'));
			//print_r($paths);exit();
			$pathsCount = count($paths);
			if ($pathsCount <= 3) {
				$_GET['action'] = array_pop($paths);
				$_GET['controller'] = array_pop($paths);
				$_GET['module'] = array_pop($paths);
			} else {
				$_GET['module'] = array_shift($paths);
				$_GET['controller'] = array_shift($paths);
				$_GET['action'] = array_shift($paths);
				//数组值个数不为偶数则补为偶数
				if (($pathsCount % 2) !== 0) {
					array_push($paths, '');
				}

				while (!empty($paths)) {
					$_GET[array_shift($paths)] = array_shift($paths);
				}
				unset($paths);
			}
			//如果为3那么使用自定义正则表达式路由规则,http://localhost/控制器_方法_其他参数 
		} elseif (SysConfig::$URL_MODE == 3) {
			if (empty(self::$rounter)) {
				$this->setRouter(AppConfig::$router);
			}
			$pathInfo = $this->getPathInfo();
			$config = self::$rounter;
			foreach ($config as $key => $value) {
				preg_match($value[0], $pathInfo, $r);
				if (!empty($r)) {
					//模块控制器等信息
					foreach ($value[1] as $key1 => $value1) {
						$_GET[$key1] = $value1;
					}
					//参数信息
					if (isset($r[1]) && !empty($value[2])) {
						foreach ($value[2] as $key2 => $value2) {
							$_GET[$value2] = $r[$key2];
						}
					}
					break;
				}
			}
		}
		$_GET['module'] = !empty($_GET['module']) ? $_GET['module'] : SysConfig::$DEFAULT_MODULE;
		$_GET['controller'] = !empty($_GET['controller']) ? $_GET['controller'] : SysConfig::$DEFAULT_CONTROL;
		$_GET['action'] = !empty($_GET['action']) ? $_GET['action'] : SysConfig::$DEFAULT_ACTION;
		//print_r($_GET); 
	}

	public function setControllerDir($controllerDir) {
		self::$controllerDir = $controllerDir;
	}

	public function setRouter($rounter) {
		self::$rounter = $rounter;
	}

	public function getPathInfo() {
		//print_r($_SERVER);
		$path = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : ($_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : '/');
		//echo $path;
		$wwwDir = str_replace('index.php', '', $path);
		//echo $_SERVER['REQUEST_URI'];
		$pathInfo = substr($_SERVER['REQUEST_URI'], strlen($wwwDir));
		//print_r($pathInfo);
		return $pathInfo;
	}

	//析构函数
	public function __destruct() {
		
	}

}